//
//  YCDistanceSensorViewController.m
//  DemoDistanceSensor
//
//  Created by 唐可雄 on 16/8/31.
//  Copyright © 2016年 swustyc. All rights reserved.
//

#import "YCDistanceSensorViewController.h"

@interface YCDistanceSensorViewController ()

@end

@implementation YCDistanceSensorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [UIDevice currentDevice].proximityMonitoringEnabled = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityStateDidChange) name:UIDeviceProximityStateDidChangeNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [UIDevice currentDevice].proximityMonitoringEnabled = NO;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)clearBtnDidClick:(id)sender {
    self.textView.text = nil;
}

- (void)proximityStateDidChange {
    if ([UIDevice currentDevice].proximityState) {
        self.textView.text = [self.textView.text stringByAppendingString:@"有物品靠近\n"];;
    } else {
        self.textView.text = [self.textView.text stringByAppendingString:@"有物品离开\n"];
    }
}

@end
